import axios from "axios";
import { notification } from "antd";
import router from "umi/router";

const codeMessage = {
  202: "一个请求已经进入后台排队（异步任务）。",
  401: "用户没有权限（令牌、用户名、密码错误）。",
  404: "发出的请求针对的是不存在的记录，服务器没有进行操作。",
  500: "服务器发生错误，请检查服务器。"
};
// axios.defaults.withCredentials = true;
axios.interceptors.request.use(function(config){
  // console.log(config.url)
  // console.log(window.location)
  // console.log(config.url.replace('/api',''))
  // if(window.location.host.indexOf('localhost')>-1){

  // }else{
  //   config.url = window.location.origin+(config.url.replace('/api',''));
  // }
  // if(config.url.indexOf('/language/')>-1){
  //   if(window.location.hostname.indexOf('xminer.io')<0){
  //     config.url = '/api'+config.url;
  //   }
  //   config.url = config.url+'?'+new Date().getTime()
  // }else{
  //   let data = {id:Date.now(),params:[]};
  //   if(config.data){
  //     data.params = Object.keys(config.data).map(function(key){return config.data[key];})
  //   }
  //   if(localStorage.getItem('token')){
  //     config.headers.token = localStorage.getItem('token');
  //   }
  //   config.data = data;
  //   config.url = 'https://testnet-api.xminer.io/'+config.url;
    
  // }
  
  return config;
})
// 仅拦截异常状态响应
axios.interceptors.response.use(function(response){
  var data = response.data;
  console.log(data)
  if(data.code!=10000){
    notification.error({
      message: `系统错误 ${data.message}`
    });
    
    if(data.code == 'token_error'){
      localStorage.removeItem('token');
      router.push('/login');
    }
  }
  return data;
}, ({ response }) => {
  console.log(response)
  // if (codeMessage[response.status]) {
  //   notification.error({
  //     message: `请求错误 ${response.status}: ${response.config.url}`,
  //     description: codeMessage[response.status]
  //   });
  // }
  return Promise.reject(response);
});
